
const fs = require('fs');
const project_config = require('../project_config');

const tool = require('../tool');

module.exports = api_generate;

function api_generate(project_config) {
  var headStr = "",
    apiStr = "",
    totalUrlStr = "";

  for (var item in project_config) {
    var pageConfig = project_config[item];
    if (item == 'default_router') continue;
    //开发路径
    if (item == 'devPath') {
      headStr += `
      // 公共路径
      // const commonPath = "${pageConfig}"; //开发
      `;
      continue;
    }
    //正式路径
    if (item == 'commonPath') {
      headStr += `
      const commonPath = "${pageConfig}"; //线上

      //接口api
      export default {

      `;
      continue;
    }
    //api添加
    for (var subItem in pageConfig) {
      if (subItem == 'api') {
        apiStr += item + ":" + pageConfig[subItem] + `,\n`;
      }
      if (subItem == 'common_api') {
        apiStr += item + ':' + pageConfig[subItem] + ',\n';
      }
    }
    totalUrlStr = headStr + apiStr + `
    }`;
  }
  
  //生成文件夹
  var makeDir = tool.testAndMakeDirectory({
    dirPath: '../src/utils'
  });

  //写入函数
  makeDir.then( () => {
    tool.writeFile({
      outputPath: '../src/utils/api_config.js',
      fileContent: totalUrlStr
    })
  });
}
